package org.jflame.context.spring.converter;

import java.util.Date;

import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;

public abstract class DateToSqlDateConverters {

    /**
     * 注册<code>java.util.Date</code>转子类Converter
     * 
     * @see DateToSqlDateConverter
     * @see DateToTimestampConverter
     * @see DateToSqlTimeConverter
     * @param registry
     */
    public static void addConverter(FormatterRegistry registry) {
        registry.addConverter(new DateToSqlDateConverter());
        registry.addConverter(new DateToTimestampConverter());
        registry.addConverter(new DateToSqlTimeConverter());
    }

    public static class DateToSqlDateConverter implements Converter<Date,java.sql.Date> {

        @Override
        public java.sql.Date convert(Date source) {
            return new java.sql.Date(source.getTime());
        }
    }

    public static class DateToTimestampConverter implements Converter<Date,java.sql.Timestamp> {

        @Override
        public java.sql.Timestamp convert(Date source) {
            return new java.sql.Timestamp(source.getTime());
        }
    }

    public static class DateToSqlTimeConverter implements Converter<Date,java.sql.Time> {

        @Override
        public java.sql.Time convert(Date source) {
            return new java.sql.Time(source.getTime());
        }
    }
}
